﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web.UI;
using DevExpress.Web.ASPxGridView;
using GPSTracker.Helper;

namespace GPSTracker.Views.Delivery
{
    public partial class ViewDeliveryEntry : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    if (Session["LoginMaster"] != "" && Session["LoginMaster"] != null)
                    {
                        var lg = Session["LoginMaster"] as Views.DAL.LoginBO;
                        DateTime dateFromDate = DateTime.Now;
                        dateFromDate = dateFromDate.AddDays(-5);

                        dtpFromDate.Value = dateFromDate;
                        dtpToDate.Value = DateTime.Now;

                        BindSqlDataSource(dateFromDate, DateTime.Now, lg.WarehouseID, lg.BranchID);
                    }
                }
            }
            catch (Exception ex)
            {
                CommonDAL.ErrorDAL.ErrorLog(ex.Message, "View Delivery Entry", "Page_Load");
                ScriptManager.RegisterStartupScript(this, typeof(Page), "alertmessage", "javascript:alert('" + Helper.ExceptionHandler.GetErrorMsg(ex) + "');", true);
            }            
        }

        public void BindSqlDataSource(DateTime FromDate, DateTime ToDate, int intWarehouseID, int  intBranchID)
        {
            try
            {
                SqlDataSource1.ConnectionString = CommonFx.GetSqlConStr();
                SqlDataSource1.SelectCommand = "EXEC dbo.SP_GetDeliveryEntry @pOrderFromDate = '" + FromDate.ToString("yyyy-MMM-dd") + "',@pOrderToDate = '" + ToDate.ToString("yyyy-MMM-dd") + "',@pWhouseID = '" + intWarehouseID + "',@pBranchID = '" + intBranchID + "' ";
                SqlDataSource1.DataBind();
                grdView.DataBind();
            }
            catch (Exception ex)
            {
                CommonDAL.ErrorDAL.ErrorLog(ex.Message, "View Delivery Entry", "BindSqlDataSource");
                ScriptManager.RegisterStartupScript(this, typeof(Page), "alertmessage", "javascript:alert('" + Helper.ExceptionHandler.GetErrorMsg(ex) + "');", true);
            }
        }

        protected void grdView_AfterPerformCallback(object sender, ASPxGridViewAfterPerformCallbackEventArgs e)
        {
            try
            {
                if (Session["LoginMaster"] != "" && Session["LoginMaster"] != null)
                {
                    var lg = Session["LoginMaster"] as Views.DAL.LoginBO;
                    if (dtpFromDate.Value == string.Empty || dtpFromDate.Value == null || dtpToDate.Value == string.Empty || dtpToDate.Value == null)
                    {
                        DateTime dateFromDate = DateTime.Now;
                        dateFromDate = dateFromDate.AddDays(-5);
                        BindSqlDataSource(dateFromDate, DateTime.Now, lg.WarehouseID, lg.BranchID);
                    }
                    else
                    {
                        BindSqlDataSource(Convert.ToDateTime(dtpFromDate.Value), Convert.ToDateTime(dtpToDate.Value), lg.WarehouseID, lg.BranchID);
                    }
                    
                }
            }
            catch (Exception ex)
            {
                CommonDAL.ErrorDAL.ErrorLog(ex.Message, "View Delivery Entry", "grdView_AfterPerformCallback");
                ScriptManager.RegisterStartupScript(this, typeof(Page), "alertmessage", "javascript:alert('" + Helper.ExceptionHandler.GetErrorMsg(ex) + "');", true);
            }
        }

        protected void grdView_CustomButtonCallback(object sender, ASPxGridViewCustomButtonCallbackEventArgs e)
        {
            try
            {
                var grid = (ASPxGridView)sender;
                var keyValue = grid.GetRowValues(e.VisibleIndex, "DeliveryEntryID").ToString();
                Session["DeliveryEntryID"] = keyValue;
                if (e.ButtonID == "btnEdit")
                {
                    SqlCommand cmd = new SqlCommand("SP_AllowEditForDeliveryEntry", CommonFx.GetSqlCon());
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandTimeout = 300000000;

                    cmd.Parameters.Add(new SqlParameter("@pDeliveryEntryID", keyValue));
                    cmd.Parameters.Add("@pIsErrorFound", SqlDbType.Bit);
                    cmd.Parameters.Add("@pErrorMessage", SqlDbType.NVarChar, 4000);
                    cmd.Parameters["@pIsErrorFound"].Direction = ParameterDirection.Output;
                    cmd.Parameters["@pErrorMessage"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();
                    var str = cmd.Parameters["@pIsErrorFound"].Value.ToString();
                    var strErrMsg = cmd.Parameters["@pErrorMessage"].Value.ToString();
                    if (!Convert.ToBoolean(cmd.Parameters["@pIsErrorFound"].Value))
                    {
                        Response.Redirect("DeliveryEntry.aspx?MODE=EDIT");
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(this, typeof(Page), "alertmessage", "javascript:alert('" + strErrMsg + "');", true);
                    }
                }
                else
                {
                    if (e.ButtonID == "btnDelete")
                    {
                        SqlCommand cmd = new SqlCommand("SP_DeleteViewDeliveryEntry", CommonFx.GetSqlCon());
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandTimeout = 300000000;

                        cmd.Parameters.Add(new SqlParameter("@pDeliveryEntryID", keyValue));
                        cmd.Parameters.Add("@pIsErrorFound", SqlDbType.Bit);
                        cmd.Parameters.Add("@pErrorMessage", SqlDbType.NVarChar, 4000);
                        cmd.Parameters["@pIsErrorFound"].Direction = ParameterDirection.Output;
                        cmd.Parameters["@pErrorMessage"].Direction = ParameterDirection.Output;

                        cmd.ExecuteNonQuery();

                        var str = cmd.Parameters["@pIsErrorFound"].Value.ToString();
                        var strErrMsg = cmd.Parameters["@pErrorMessage"].Value.ToString();

                        if (!Convert.ToBoolean(cmd.Parameters["@pIsErrorFound"].Value))
                        {

                        }
                        else
                        {
                            ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert('" + strErrMsg + "');", true);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                CommonDAL.ErrorDAL.ErrorLog(ex.Message, "View Delivery Entry", "grdView_CustomButtonCallback");
                ScriptManager.RegisterStartupScript(this, typeof(Page), "alertmessage", "javascript:alert('" + Helper.ExceptionHandler.GetErrorMsg(ex) + "');", true);
            }
        }

        protected void btnDisplay_Click(object sender, EventArgs e)
        {
            try
            {
                if (Session["LoginMaster"] != "" && Session["LoginMaster"] != null)
                {
                    var lg = Session["LoginMaster"] as Views.DAL.LoginBO;
                    BindSqlDataSource(Convert.ToDateTime(dtpFromDate.Value), Convert.ToDateTime(dtpToDate.Value), lg.WarehouseID, lg.BranchID);
                }
            }
            catch (Exception ex)
            {
                CommonDAL.ErrorDAL.ErrorLog(ex.Message, "View Delivery Entry", "btnDisplay_Click");
                ScriptManager.RegisterStartupScript(this, typeof(Page), "alertmessage", "javascript:alert('" + Helper.ExceptionHandler.GetErrorMsg(ex) + "');", true);
            }
        }



        //-- CODE END
    }
}
